package baduga.karo;


import java.awt.Color;
import java.awt.Toolkit;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.border.*;
import javax.swing.BorderFactory;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.Timer;
import javax.swing.WindowConstants;

import java.awt.Font;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JMenuItem;

import baduga.client.Controler;
import baduga.client.Player_Constans;
import baduga.core.Core_Gui_ErrorWindow;
import baduga.core.Core_JsonDataSchema;
import baduga.core.Core_Messages;
/**
* @author Karolina
*/
public class TableGame extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;
	
	//na czas testów to 1
	
	/** nr wew. Dealera (informacja kto był ostatnim) */
	int dealerNumber = -1;
	
	
	//jak będzie true to pokazujemy all-in, fold itd..
	boolean myTurn = false;
	
	private Controler contr;
	
	private static Timer playerTimeToMove;

	
	//ścieżka do plików graficznych - kart, ikonek itd
	protected static final String RESOURCE_DIR = "resources/";
	
	  private JButton jButtonAllIn;
	  private JButton jButtonBet;
	  private JButton jButtonCall;
	  private JButton jButtonCheck;
	  private JButton jButtonFold;
	  private JButton jButtonRaise;
	  private JButton jButtonSit;
	  private JButton jButtonStart;

	  private JLabel jLabel16;
	  private JLabel jLabel17;
	  private JLabel jLabel18;
	  private JLabel jLabel19;

	  private JLabel jLabel21;

	  private JLabel adminName;
	  private JLabel dealerName;
	  private JLabel nrOfDeal;
	  private JLabel jLabel34;
	  private JLabel potValue;
	  private JLabel jLabel36;
	  private JLabel jLabel38;

	  private JMenuBar jMenuBar1;
	  private JMenu jMenuFIle;
	  private JMenu jMenuGame;
	  private JMenu jMenuHelp;
	  private JMenuItem jMenuItem1;
	  private JMenuItem jMenuItemAbout;
	  private JMenuItem jMenuItemExit;
	  private JMenuItem jMenuItemHelp;
	  private JMenuItem jMenuItemSit;
	  private JMenuItem jMenuItemStart;
	  private JPanel jPanel1;
	  private JPanel jPanel2;
	  private JPanel jPanel3;
	  private JPanel jPanel4;
	  private JPanel jPanel5;
	  private JPanel jPanel6;
	  private JPanel jPanel7;
	  private JPanel jPanel8;
	  private JScrollPane jScrollPane1;
	  private JTextArea gameLog;
	  public static JLabel tileETMove = new JLabel("");
	 
	
	  //Labele które są graficznym uosobieniem karty gracza
	  private JLabel[] labelPlayerCards = new JLabel[4];
	  
	  //Przycisk DealerButton pozycje dla graczy
	  private JLabel[] dealerInf = new JLabel[6];
	  
	  
	  //Labele odpowiadają za nickname
	  private JLabel[] playerName = new JLabel[6];
	  
	  //Labele odpowiadają za Cash
	  private JLabel[] playerCash = new JLabel[6];
	  
	  //AccessList buttonów
	  private JLabel[] playerLastMove = new JLabel[6];
	  
	  //Zaklad gracza
	  private JLabel[] playerBid = new JLabel[6];
	  
	  
	  /**
	   * Fonty:
	   * 
	   * <pre>
	   * [0] - Nazwy innych graczy
	   * [1] - Ruchy innych graczy
	   * [2] - Nazwa gracza
	   * [3] - Ruch gracza
	   * [4] - Czcionka w GameLogu
	   * </pre>
	   */
	  private Font[] labelFont = new Font[5];

	  
	  
	  /**
	   * Zawiera informacje o stanie gry:
	   * 
	   * <pre>
	   * [0]
	   * 0 -  Obserwator
	   * 1 - Gracz
	   * 
	   * [1] - stan gry
	   * 0 - inicjalizacja gry
	   * 1 - mój ruch
	   * 2 - wymiana kart
	   * 3 - Dealer
	   * 4 - Licytacja
	   * 
	   * [2] dot. (Czynności cyklicznych)
	   * 0 - Start
	   * 1 - End
	   * </pre>
	   * 
	   */
	  int gameState[] = new int[3];

	  /** Czy gra jest w toku*/
	  boolean gameStart = false;
	  
	  
	  
	  
	  
	  
	  
	
	  

	  /**
	   * Czy karta została namaszczona do wymiany:
	   * <pre>
	   * 0 - TAK
	   * 1 - Nie
	   * </pre>
	   * 
	   */
	  int[] accessMod = {1,1,1,1};
	 
	  
	  
	  
	  
	  private JButton btnDealer;
	  private JButton btnChangeCards;
	  
	  /** Ilość kart do przeniesienia */
	  int suma = 0;
	  private JMenuItem mntmOpuSt;
	  private JButton btnNieWymieniam;
	  
	  
	  

  //tworzymy nowy stół
	
  public TableGame(Controler newVar, boolean isCreator) {
	  
	  contr = newVar;
	  
	  

	  initComponents();
       

      //Ustawienia Elementów stołu
      potValue.setText( this.updateCash("0")  );
      
      //Ustawia znacznik
      //chooseDealer(dealerNumber);
      
      
      

      
      //Inicjalizacja fontów
      labelFont[0] = new Font("Tahoma", 1, 14);
      labelFont[1] = new Font("Tahoma", 1, 11);
      labelFont[2] = new Font("Arial", 1, 16); 
      labelFont[3] = new Font("Tahoma", 1, 11);
      labelFont[4] = new Font("Arial", Font.PLAIN, 12);
      
      
      //Def.
      int size;
      
      
      //Init.
      size = playerLastMove.length;
      
      if(isCreator)
      {
    	  size -= 1;
    	  
    	  //Flagi stołu
    	  gameState[0]  = 1;
      }
      else
      {
    	  gameState[0]  = 0;
      }

      

      //default
      for(int i = 0; i < size; i++)
      {

    	  playerName[i] 	= new JLabel( Core_Messages.getMessage(0x0001)  );
    	  playerCash[i] 	= new JLabel( this.updateCash("0"));
    	  playerLastMove[i] = new JLabel( Core_Messages.getMessage(0x0001) );
    	  playerBid[i] 		= new JLabel( this.updateCash("0"));
    	  
    	  //CFG
    	  
    	  
    	  playerName[i].setFont(labelFont[0]);
    	  playerLastMove[i].setFont(labelFont[1]);
    	  
      }
      



      if(isCreator)
      {
    	  playerName[size] 		= new JLabel( Player_Constans.getPlayerNickname() );
    	  playerCash[size] 		= new JLabel( this.updateCash( Integer.toString(Player_Constans.getCash()) ) );
    	  playerLastMove[size] 	= new JLabel( moveTranslator(new Integer[] {-1, 0}) );
    	  playerBid[size] 		= new JLabel( this.updateCash("0"));
      }
      else
      {
         size -= 1;
      }
      

      //CFG
      playerName[size].setFont( labelFont[2] );
      playerCash[size].setFont( labelFont[3] );
      playerLastMove[size].setFont( labelFont[3] );

      
      //GameLog
      
      
      initTable(isCreator);
      
      prepareEnviroment();
  }
  
  
  
  
  
  private final void initTable(boolean isCreator)
  {
	  	setIconImage(Toolkit.getDefaultToolkit().getImage(TableGame.class.getResource("/baduga/karo/resources/icon.png")));
	     
	      
	      setUpComponents(isCreator);
	      
	      //Ustawia karty 
	      for(int i = 0; i < 4; i++)
	      {
	    	  setCardImage(new int[] {0, i}, null, "card_back");
	      }

   	  
	     	  
	     	  
	     	  //TODO: Czas będzie brany od serwera.
	     	 /**
	     	  * Odliczacz ile zostąłu do końca ruchu... gracza. Może do bankructwa?
	     	  */
	     	  
	     	 //final Thread moveTimerThread = new Thread(new TimerThread(60, tileETMove));
	     	 //moveTimerThread.start();
	     	 
	     	 //playerTimeToMove.setInitialDelay(1000);
	     	  

	     	  
	     	  
	     	 //playerTimeToMove.start();
	     	  

	       
	       

	       
  }
  
  
  
  
  
  
  
  

  
 
  
  
  @SuppressWarnings("unchecked")
  
  	//tworzymy podstawowe elementy graficzne stołu
  
  /**
   * 
   * Inicjalizuje Komponenty
   * 
   */
  private void initComponents() {


      jMenuItem1 = new JMenuItem();
      jPanel1 = new JPanel();
      jLabel16 = new JLabel();
      jPanel2 = new JPanel();
      labelPlayerCards[0] = new JLabel();
      labelPlayerCards[1] = new JLabel();
      labelPlayerCards[2] = new JLabel();
      labelPlayerCards[3] = new JLabel();
      jPanel3 = new JPanel();
      jLabel17 = new JLabel();
      jPanel4 = new JPanel();
      jLabel18 = new JLabel();
      jPanel5 = new JPanel();
      jLabel19 = new JLabel();
      jPanel6 = new JPanel();
      jLabel21 = new JLabel();
      jPanel7 = new JPanel();
      nrOfDeal = new JLabel();
      dealerName = new JLabel();
      adminName = new JLabel();
      jPanel8 = new JPanel();
      jLabel34 = new JLabel();
      potValue = new JLabel();
      jLabel36 = new JLabel();
      jLabel38 = new JLabel();
      jButtonAllIn = new JButton();
      jButtonRaise = new JButton();
      jButtonCheck = new JButton();
      jButtonFold = new JButton();
      jButtonCall = new JButton();
      jButtonBet = new JButton();
      jScrollPane1 = new JScrollPane();
      jButtonSit = new JButton();
      jButtonStart = new JButton();
      jMenuBar1 = new JMenuBar();
      jMenuFIle = new JMenu();
      jMenuItemExit = new JMenuItem();
      jMenuGame = new JMenu();
      jMenuItemSit = new JMenuItem();
      jMenuItemStart = new JMenuItem();
      jMenuHelp = new JMenu();
      jMenuItemHelp = new JMenuItem();
      jMenuItemAbout = new JMenuItem();
      gameLog = new JTextArea();
      
      
      //rozmiary okna  i ustalenie rozmieszczenia elementów na stole
      //ułożenie graczy, ich nicków, stanów konta - cała szata graficzna gru
      
      setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
      setTitle("Badugi - Stół do gry");
      setMinimumSize(new java.awt.Dimension(900, 640));
      setPreferredSize(new java.awt.Dimension(900, 640));
      setResizable(false);
      setVisible(true);
      
      

      
      
      gameLog.setFont( labelFont[4] );
      gameLog.setLineWrap(true);
      gameLog.setWrapStyleWord(true);
      
      //Wymiary
      gameLog.setColumns(20);
      gameLog.setRows(5);
      
      gameLog.setEditable(false);

      

  }
      

  /**
   * Konfiguruje komponenty
   * 
   */
  private void setUpComponents(boolean isCreator) {
	  
	  
	  
	  
      if(isCreator)
      {
    	  nrOfDeal.setText("Ilość rozdań: " + Core_Messages.getMessage(0x0001));

    	  dealerName.setText("Dealer: " + Core_Messages.getMessage(0x0001));

    	  adminName.setText("Admin: " + Player_Constans.getPlayerNickname() );
      }
	  
	  
	  
      //Czas pozostały do końca rundy
      tileETMove.setText( Core_Messages.getMessage(0x0001) );


      jLabel16.setIcon(new ImageIcon(getClass().getResource(RESOURCE_DIR + "card_back.png"))); 
      

      dealerInf[2] = new JLabel("New label");
      dealerInf[2].setIcon(new ImageIcon(TableGame.class.getResource("/baduga/karo/resources/button_present.png")));
      dealerInf[2].setVisible(false);
      


      GroupLayout jPanel1Layout = new GroupLayout(jPanel1);
      jPanel1Layout.setHorizontalGroup(
      	jPanel1Layout.createParallelGroup(Alignment.TRAILING)
      		.addGroup(jPanel1Layout.createSequentialGroup()
      			.addContainerGap(36, Short.MAX_VALUE)
      			.addGroup(jPanel1Layout.createParallelGroup(Alignment.TRAILING)
      				.addComponent(playerLastMove[2], GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
      				.addGroup(jPanel1Layout.createSequentialGroup()
      					.addComponent(dealerInf[2], GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
      					.addPreferredGap(ComponentPlacement.RELATED)
      					.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING)
      						.addComponent(playerCash[2], GroupLayout.PREFERRED_SIZE, 74, GroupLayout.PREFERRED_SIZE)
      						.addComponent(jLabel16)
      						.addComponent(playerName[2]))))
      			.addGap(32))
      );
      jPanel1Layout.setVerticalGroup(
      	jPanel1Layout.createParallelGroup(Alignment.LEADING)
      		.addGroup(jPanel1Layout.createSequentialGroup()
      			.addContainerGap()
      			.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING)
      				.addComponent(dealerInf[2])
      				.addGroup(jPanel1Layout.createSequentialGroup()
      					.addComponent(playerName[2])
      					.addPreferredGap(ComponentPlacement.RELATED)
      					.addGroup(jPanel1Layout.createParallelGroup(Alignment.TRAILING)
      						.addGroup(jPanel1Layout.createSequentialGroup()
      							.addComponent(jLabel16)
      							.addPreferredGap(ComponentPlacement.RELATED)
      							.addComponent(playerLastMove[2], GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
      							.addGap(159))
      						.addGroup(jPanel1Layout.createSequentialGroup()
      							.addComponent(playerCash[2])
      							.addGap(290)))))
      			.addGap(159))
      );
      jPanel1.setLayout(jPanel1Layout);

      
      
      
      /**
       * 
       * Przechwytywanie akcji użytkownika
       * 
       * TODO: Automatyzacja generowania
       * 
       */
      
      
      labelPlayerCards[0].addMouseListener(new java.awt.event.MouseAdapter() {
          public void mouseClicked(java.awt.event.MouseEvent evt) {
              labelPlayerCards0MouseClicked(evt);
          }
      });

      
      labelPlayerCards[1].addMouseListener(new java.awt.event.MouseAdapter() {
          public void mouseClicked(java.awt.event.MouseEvent evt) {
        	  labelPlayerCards1MouseClicked(evt);
          }
      });

      
      labelPlayerCards[2].addMouseListener(new java.awt.event.MouseAdapter() {
          public void mouseClicked(java.awt.event.MouseEvent evt) {
        	  labelPlayerCards2MouseClicked(evt);
          }
      });

      
      labelPlayerCards[3].addMouseListener(new java.awt.event.MouseAdapter() {
          public void mouseClicked(java.awt.event.MouseEvent evt) {
        	  labelPlayerCards3MouseClicked(evt);
          }
      });
      
      
      

      GroupLayout jPanel2Layout = new GroupLayout(jPanel2);
      jPanel2.setLayout(jPanel2Layout);
      jPanel2Layout.setHorizontalGroup(
          jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
          .addGroup(jPanel2Layout.createSequentialGroup()
              .addGap(0, 0, 0)
              .addComponent(labelPlayerCards[0], GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
              .addGap(8, 8, 8)
              .addComponent(labelPlayerCards[1])
              .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
              .addComponent(labelPlayerCards[2], GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
              .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              .addComponent(labelPlayerCards[3])
              .addContainerGap())
      );
      jPanel2Layout.setVerticalGroup(
          jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
          .addComponent(labelPlayerCards[1], GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addGroup(GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
              .addContainerGap()
              .addComponent(labelPlayerCards[0], GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
              .addContainerGap())
          .addComponent(labelPlayerCards[3], GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
          .addComponent(labelPlayerCards[2], GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      );
      
      
      


      jLabel17.setIcon(new ImageIcon(getClass().getResource(RESOURCE_DIR + "card_back.png"))); 

      
      dealerInf[1] = new JLabel("New label");
      dealerInf[1].setIcon(new ImageIcon(TableGame.class.getResource("/baduga/karo/resources/button_present.png")));
      dealerInf[1].setVisible(false);


      GroupLayout jPanel3Layout = new GroupLayout(jPanel3);
      jPanel3Layout.setHorizontalGroup(
      	jPanel3Layout.createParallelGroup(Alignment.TRAILING)
      		.addGroup(jPanel3Layout.createSequentialGroup()
      			.addContainerGap(21, Short.MAX_VALUE)
      			.addGroup(jPanel3Layout.createParallelGroup(Alignment.TRAILING)
      				.addComponent(playerLastMove[1], GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
      				.addGroup(jPanel3Layout.createSequentialGroup()
      					.addComponent(dealerInf[1], GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
      					.addPreferredGap(ComponentPlacement.RELATED)
      					.addGroup(jPanel3Layout.createParallelGroup(Alignment.LEADING)
      						.addComponent(playerCash[1], GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
      						.addComponent(jLabel17)
      						.addComponent(playerName[1]))))
      			.addGap(19))
      );
      jPanel3Layout.setVerticalGroup(
      	jPanel3Layout.createParallelGroup(Alignment.LEADING)
      		.addGroup(jPanel3Layout.createSequentialGroup()
      			.addContainerGap()
      			.addGroup(jPanel3Layout.createParallelGroup(Alignment.LEADING)
      				.addGroup(jPanel3Layout.createSequentialGroup()
      					.addComponent(playerName[1])
      					.addPreferredGap(ComponentPlacement.RELATED)
      					.addComponent(playerCash[1])
      					.addGap(11)
      					.addComponent(jLabel17))
      				.addComponent(dealerInf[1]))
      			.addPreferredGap(ComponentPlacement.UNRELATED)
      			.addComponent(playerLastMove[1], GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
      			.addContainerGap(29, Short.MAX_VALUE))
      );
      jPanel3.setLayout(jPanel3Layout);

      

      


      jLabel18.setIcon(new ImageIcon(getClass().getResource(RESOURCE_DIR + "card_back.png"))); 
      
      
      
      dealerInf[0] = new JLabel("New label");
      dealerInf[0].setIcon(new ImageIcon(TableGame.class.getResource("/baduga/karo/resources/button_present.png")));
      dealerInf[0].setVisible(false);
      
      dealerInf[5] = new JLabel("New label");
      dealerInf[5].setIcon(new ImageIcon(TableGame.class.getResource("/baduga/karo/resources/button_present.png")));
      dealerInf[5].setVisible(false);

      
      
      
      
      GroupLayout jPanel4Layout = new GroupLayout(jPanel4);
      jPanel4Layout.setHorizontalGroup(
      	jPanel4Layout.createParallelGroup(Alignment.LEADING)
      		.addGroup(jPanel4Layout.createSequentialGroup()
      			.addGap(16)
      			.addGroup(jPanel4Layout.createParallelGroup(Alignment.TRAILING)
      				.addComponent(playerLastMove[0])
      				.addGroup(jPanel4Layout.createSequentialGroup()
      					.addComponent(dealerInf[0], GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
      					.addGap(12)
      					.addGroup(jPanel4Layout.createParallelGroup(Alignment.TRAILING)
      						.addComponent(playerName[0], Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
      						.addGroup(jPanel4Layout.createParallelGroup(Alignment.LEADING)
      							.addComponent(playerCash[0], GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
      							.addComponent(jLabel18)))))
      			.addContainerGap(18, Short.MAX_VALUE))
      );
      jPanel4Layout.setVerticalGroup(
      	jPanel4Layout.createParallelGroup(Alignment.LEADING)
      		.addGroup(jPanel4Layout.createSequentialGroup()
      			.addContainerGap()
      			.addGroup(jPanel4Layout.createParallelGroup(Alignment.LEADING)
      				.addGroup(jPanel4Layout.createSequentialGroup()
      					.addComponent(playerName[0], GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE)
      					.addPreferredGap(ComponentPlacement.RELATED)
      					.addComponent(playerCash[0])
      					.addPreferredGap(ComponentPlacement.RELATED)
      					.addComponent(jLabel18))
      				.addComponent(dealerInf[0]))
      			.addPreferredGap(ComponentPlacement.RELATED)
      			.addComponent(playerLastMove[0], GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
      			.addContainerGap(34, Short.MAX_VALUE))
      );
      jPanel4.setLayout(jPanel4Layout);
      
      


      jLabel19.setIcon(new ImageIcon(getClass().getResource(RESOURCE_DIR + "card_back.png"))); 


      dealerInf[4] = new JLabel("New label");
      dealerInf[4].setIcon(new ImageIcon(TableGame.class.getResource("/baduga/karo/resources/button_present.png")));
      dealerInf[4].setVisible(false);


      GroupLayout jPanel5Layout = new GroupLayout(jPanel5);
      jPanel5Layout.setHorizontalGroup(
      	jPanel5Layout.createParallelGroup(Alignment.TRAILING)
      		.addGroup(jPanel5Layout.createSequentialGroup()
      			.addGap(48)
      			.addGroup(jPanel5Layout.createParallelGroup(Alignment.TRAILING)
      				.addComponent(playerLastMove[4])
      				.addComponent(jLabel19)
      				.addGroup(jPanel5Layout.createSequentialGroup()
      					.addComponent(dealerInf[4], GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
      					.addGap(10)
      					.addGroup(jPanel5Layout.createParallelGroup(Alignment.LEADING)
      						.addComponent(playerName[4], GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
      						.addComponent(playerCash[4], GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE))))
      			.addContainerGap(43, Short.MAX_VALUE))
      );
      jPanel5Layout.setVerticalGroup(
      	jPanel5Layout.createParallelGroup(Alignment.TRAILING)
      		.addGroup(jPanel5Layout.createSequentialGroup()
      			.addContainerGap(11, Short.MAX_VALUE)
      			.addGroup(jPanel5Layout.createParallelGroup(Alignment.LEADING)
      				.addGroup(jPanel5Layout.createSequentialGroup()
      					.addGap(23)
      					.addComponent(playerCash[4])
      					.addPreferredGap(ComponentPlacement.RELATED)
      					.addComponent(jLabel19))
      				.addComponent(playerName[4])
      				.addComponent(dealerInf[4]))
      			.addPreferredGap(ComponentPlacement.RELATED)
      			.addComponent(playerLastMove[4], GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
      			.addGap(27))
      );
      jPanel5.setLayout(jPanel5Layout);



      jLabel21.setIcon(new ImageIcon(getClass().getResource(RESOURCE_DIR + "card_back.png"))); 


      dealerInf[3] = new JLabel("New label");
      dealerInf[3].setIcon(new ImageIcon(TableGame.class.getResource("/baduga/karo/resources/button_present.png")));
      dealerInf[3].setVisible(false);

            
      

      GroupLayout jPanel6Layout = new GroupLayout(jPanel6);
      jPanel6Layout.setHorizontalGroup(
      	jPanel6Layout.createParallelGroup(Alignment.TRAILING)
      		.addGroup(jPanel6Layout.createSequentialGroup()
      			.addContainerGap(41, Short.MAX_VALUE)
      			.addGroup(jPanel6Layout.createParallelGroup(Alignment.TRAILING)
      				.addComponent(jLabel21)
      				.addComponent(playerLastMove[3], GroupLayout.PREFERRED_SIZE, 99, GroupLayout.PREFERRED_SIZE)
      				.addGroup(jPanel6Layout.createSequentialGroup()
      					.addComponent(dealerInf[3], GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
      					.addPreferredGap(ComponentPlacement.RELATED)
      					.addGroup(jPanel6Layout.createParallelGroup(Alignment.LEADING)
      						.addComponent(playerName[3])
      						.addComponent(playerCash[3], GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE))))
      			.addGap(18))
      );
      jPanel6Layout.setVerticalGroup(
      	jPanel6Layout.createParallelGroup(Alignment.TRAILING)
      		.addGroup(jPanel6Layout.createSequentialGroup()
      			.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      			.addGroup(jPanel6Layout.createParallelGroup(Alignment.LEADING)
      				.addGroup(jPanel6Layout.createSequentialGroup()
      					.addGap(23)
      					.addComponent(playerCash[3]))
      				.addGroup(jPanel6Layout.createParallelGroup(Alignment.BASELINE)
      					.addComponent(playerName[3])
      					.addComponent(dealerInf[3])))
      			.addPreferredGap(ComponentPlacement.RELATED)
      			.addComponent(jLabel21)
      			.addPreferredGap(ComponentPlacement.RELATED)
      			.addComponent(playerLastMove[3], GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
      			.addGap(35))
      );
      jPanel6.setLayout(jPanel6Layout);

      jPanel7.setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));



      GroupLayout jPanel7Layout = new GroupLayout(jPanel7);
      jPanel7Layout.setHorizontalGroup(
      	jPanel7Layout.createParallelGroup(Alignment.LEADING)
      		.addGroup(jPanel7Layout.createSequentialGroup()
      			.addContainerGap()
      			.addGroup(jPanel7Layout.createParallelGroup(Alignment.LEADING)
      				.addComponent(nrOfDeal, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE)
      				.addComponent(adminName, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE)
      				.addComponent(dealerName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
      			.addContainerGap(37, Short.MAX_VALUE))
      );
      jPanel7Layout.setVerticalGroup(
      	jPanel7Layout.createParallelGroup(Alignment.LEADING)
      		.addGroup(jPanel7Layout.createSequentialGroup()
      			.addContainerGap()
      			.addComponent(nrOfDeal, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
      			.addPreferredGap(ComponentPlacement.RELATED)
      			.addComponent(dealerName, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
      			.addPreferredGap(ComponentPlacement.RELATED)
      			.addComponent(adminName, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
      			.addContainerGap(20, Short.MAX_VALUE))
      );
      jPanel7.setLayout(jPanel7Layout);

      jPanel8.setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

      jLabel34.setFont(new Font("Arial", 1, 18)); 
      jLabel34.setText("Pula");

      

      jLabel36.setFont(new Font("Arial", 3, 12));
      jLabel36.setIcon(new ImageIcon(getClass().getResource(RESOURCE_DIR + "icon.png"))); 
      jLabel36.setText("BADUGI");
      jLabel36.setBorder(BorderFactory.createCompoundBorder());

      jLabel38.setText("Pozostały czas na ruch");


      
      //giveElapsedTime();
     
      
      
      
      GroupLayout jPanel8Layout = new GroupLayout(jPanel8);
      jPanel8Layout.setHorizontalGroup(
      	jPanel8Layout.createParallelGroup(Alignment.LEADING)
      		.addGroup(jPanel8Layout.createSequentialGroup()
      			.addContainerGap()
      			.addGroup(jPanel8Layout.createParallelGroup(Alignment.LEADING)
      				.addGroup(Alignment.TRAILING, jPanel8Layout.createSequentialGroup()
      					.addGap(0, 19, Short.MAX_VALUE)
      					.addGroup(jPanel8Layout.createParallelGroup(Alignment.TRAILING)
      						.addGroup(jPanel8Layout.createSequentialGroup()
      							.addComponent(jLabel34)
      							.addGap(42))
      						.addGroup(jPanel8Layout.createSequentialGroup()
      							.addComponent(jLabel36, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE)
      							.addContainerGap())))
      				.addGroup(Alignment.TRAILING, jPanel8Layout.createSequentialGroup()
      					.addComponent(potValue, GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
      					.addContainerGap())
      				.addGroup(jPanel8Layout.createSequentialGroup()
      					.addComponent(jLabel38, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      					.addContainerGap())
      				.addGroup(jPanel8Layout.createSequentialGroup()
      					.addComponent(tileETMove, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
      					.addContainerGap())))
      );
      jPanel8Layout.setVerticalGroup(
      	jPanel8Layout.createParallelGroup(Alignment.LEADING)
      		.addGroup(jPanel8Layout.createSequentialGroup()
      			.addContainerGap(12, Short.MAX_VALUE)
      			.addComponent(jLabel36, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
      			.addPreferredGap(ComponentPlacement.UNRELATED)
      			.addComponent(jLabel34)
      			.addGap(1)
      			.addComponent(potValue, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
      			.addPreferredGap(ComponentPlacement.RELATED)
      			.addComponent(jLabel38, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
      			.addPreferredGap(ComponentPlacement.UNRELATED)
      			.addComponent(tileETMove)
      			.addGap(21))
      );
      jPanel8.setLayout(jPanel8Layout);



      //dopisanie akcji przyciskom odpowiadającym za najważniejsze akcje gry
      
      jButtonAllIn.setText("All-in");
      jButtonAllIn.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              jButtonAllInActionPerformed(evt);
          }
      });
     
      jButtonRaise.setText("Raise");
      jButtonRaise.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              jButtonRaiseActionPerformed(evt);
              
          }
      });

      jButtonCheck.setText("Check");
      jButtonCheck.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              jButtonCheckActionPerformed(evt);
          }
      });

      jButtonFold.setText("Fold");
      jButtonFold.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              jButtonFoldActionPerformed(evt);
          }
      });

      jButtonCall.setText("Call");
      jButtonCall.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              jButtonCallActionPerformed(evt);
          }
      });

      jButtonBet.setText("Bet");
      jButtonBet.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              jButtonBetActionPerformed(evt);
          }
      });

      
      


      //przyciski "start" i "usiądź" - start pokazuje się dopiero jak ktoś usiądzie
      //"usiądź" wtedy znika
      
      jButtonSit.setText("Usiądź");
      jButtonSit.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              jButtonSitActionPerformed(evt);
          }
      });

      jMenuItemSit.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              jMenuItemSitActionPerformed(evt);
          }
      });
      
      jButtonStart.setText("START");
      jButtonStart.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              jButtonStartActionPerformed(evt);
          }
      });
      
      jMenuItemStart.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              jMenuItemStartActionPerformed(evt);
          }
      });

      //obsługa menu pod paskiem tytułu i jego pozycji
      
      jMenuFIle.setText("Plik");

      jMenuItemExit.setText("Zakończ grę");
      jMenuItemExit.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              jMenuItemExitActionPerformed(evt);
          }
      });
      jMenuFIle.add(jMenuItemExit);

      jMenuBar1.add(jMenuFIle);

      jMenuGame.setText("Gra");

      jMenuItemSit.setText("Usiądź");
      jMenuGame.add(jMenuItemSit);

      jMenuItemStart.setText("Start");
      jMenuItemStart.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              jMenuItemStartActionPerformed(evt);
          }
      });
      jMenuGame.add(jMenuItemStart);
      jMenuItemStart.setEnabled(false);
      jMenuBar1.add(jMenuGame);
      
      mntmOpuSt = new JMenuItem("Opuść stół");
      mntmOpuSt.addActionListener(new ActionListener() {
    	  public void actionPerformed(ActionEvent evt) {
              mntmOpuStActionPerformed(evt);
          }
      });
      
      jMenuGame.add(mntmOpuSt);

      jMenuHelp.setText("Help");

      jMenuItemHelp.setText("Help");
      jMenuItemHelp.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              jMenuItemHelpActionPerformed(evt);
          }
      });
      jMenuHelp.add(jMenuItemHelp);

      jMenuItemAbout.setText("About");
      jMenuItemAbout.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
              jMenuItemAboutActionPerformed(evt);
          }
      });
      jMenuHelp.add(jMenuItemAbout);

      jMenuBar1.add(jMenuHelp);

      setJMenuBar(jMenuBar1);
      //rozdaje karty
      btnDealer = new JButton("Rozdaj");
      
      btnDealer.addActionListener(new ActionListener() {
      	public void actionPerformed(ActionEvent arg0) {
        	  if(contr.fSendData( playerMoveData(0x07, null ) ))
        	  {
        		  //Ustawienie końca etapu ruchu
        		  gameState[2] = 1;
        		  prepareEnviroment();
        	  }
        	  else
        	  {
        		  Core_Gui_ErrorWindow.errorWindow(Core_Messages.getMessage(0x02F3), Core_Messages.getMessage(0x0100));
        	  }
      	}
      });

      
      
      
      btnChangeCards = new JButton("Wymień");
      btnChangeCards.addActionListener(new ActionListener() {
      	public void actionPerformed(ActionEvent arg0) {
      		
      		
      	  if(contr.fSendData( playerMoveData(0x08, exchangeCards() ) ))
      	  {
       		  //Ustawienie końca etapu ruchu
    		  gameState[2] = 1;
    		  prepareEnviroment();
      	  }
      	  else
    	  {
    		  Core_Gui_ErrorWindow.errorWindow(Core_Messages.getMessage(0x02F3), Core_Messages.getMessage(0x0100));
    	  }

      		
      		
      	}
      });
      
      btnChangeCards.setEnabled(false);
      
      btnNieWymieniam = new JButton("Nie wymieniam");
      btnNieWymieniam.setEnabled(false);
      btnNieWymieniam.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        	if(contr.fSendData( playerMoveData(0x08, null ) ))
        	{
        		//Ustawienie końca etapu ruchu
      		  	gameState[2] = 1;
      		  	prepareEnviroment();
        	}
        	else
        	{
        		Core_Gui_ErrorWindow.errorWindow(Core_Messages.getMessage(0x02F3), Core_Messages.getMessage(0x0100));
            }
        }
      });

      
      GroupLayout layout = new GroupLayout(getContentPane());
      layout.setHorizontalGroup(
      	layout.createParallelGroup(Alignment.TRAILING)
      		.addGroup(layout.createSequentialGroup()
      			.addContainerGap()
      			.addGroup(layout.createParallelGroup(Alignment.LEADING)
      				.addGroup(layout.createSequentialGroup()
      					.addGroup(layout.createParallelGroup(Alignment.LEADING)
      						.addComponent(jPanel7, GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE)
      						.addGroup(layout.createSequentialGroup()
      							.addGap(6)
      							.addGroup(layout.createParallelGroup(Alignment.LEADING)
      								.addGroup(layout.createSequentialGroup()
      									.addComponent(jButtonStart, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
      									.addPreferredGap(ComponentPlacement.RELATED)
      									.addComponent(jButtonSit, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE))
      								.addGroup(layout.createSequentialGroup()
      									.addGroup(layout.createParallelGroup(Alignment.TRAILING)
      										.addComponent(jButtonAllIn, GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
      										.addComponent(jButtonCheck, GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE)
      										.addComponent(jButtonBet, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE))
      									.addPreferredGap(ComponentPlacement.RELATED)
      									.addGroup(layout.createParallelGroup(Alignment.LEADING)
      										.addComponent(jButtonFold, GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
      										.addComponent(jButtonRaise, GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
      										.addComponent(jButtonCall, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)))
      								.addComponent(btnDealer, GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE))))
      					.addGap(18))
      				.addGroup(layout.createSequentialGroup()
      					.addComponent(jPanel5, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE)
      					.addPreferredGap(ComponentPlacement.RELATED)))
      			.addGroup(layout.createParallelGroup(Alignment.LEADING)
      				.addGroup(layout.createSequentialGroup()
      					.addGap(353)
      					.addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 259, GroupLayout.PREFERRED_SIZE))
      				.addGroup(layout.createSequentialGroup()
      					.addGroup(layout.createParallelGroup(Alignment.TRAILING)
      						.addGroup(layout.createSequentialGroup()
      							.addGroup(layout.createParallelGroup(Alignment.LEADING)
      								.addGroup(layout.createSequentialGroup()
      									.addComponent(dealerInf[5], GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
      									.addPreferredGap(ComponentPlacement.UNRELATED)
      									.addGroup(layout.createParallelGroup(Alignment.LEADING)
      											.addComponent(playerCash[5], GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
      											.addComponent(playerName[5], GroupLayout.PREFERRED_SIZE, 130, GroupLayout.PREFERRED_SIZE))
      									.addGap(44))
      								.addComponent(playerLastMove[5], GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE))
      							.addPreferredGap(ComponentPlacement.UNRELATED)
      							.addGroup(layout.createParallelGroup(Alignment.TRAILING, false)
      								.addComponent(btnNieWymieniam, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      								.addComponent(btnChangeCards, GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)))
      						.addGroup(layout.createParallelGroup(Alignment.LEADING)
      							.addComponent(jPanel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
      							.addGroup(layout.createSequentialGroup()
      								.addComponent(jPanel4, GroupLayout.PREFERRED_SIZE, 147, GroupLayout.PREFERRED_SIZE)
      								.addGap(18)
      								.addComponent(jPanel3, GroupLayout.PREFERRED_SIZE, 147, GroupLayout.PREFERRED_SIZE))))
      					.addPreferredGap(ComponentPlacement.RELATED)
      					.addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, 159, GroupLayout.PREFERRED_SIZE)
      					.addGap(18)
      					.addGroup(layout.createParallelGroup(Alignment.LEADING)
      						.addGroup(layout.createSequentialGroup()
      							.addComponent(jPanel6, 0, 0, Short.MAX_VALUE)
      							.addPreferredGap(ComponentPlacement.RELATED))
      						.addComponent(jPanel8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
      			.addGap(17))
      );
      layout.setVerticalGroup(
      	layout.createParallelGroup(Alignment.LEADING)
      		.addGroup(layout.createSequentialGroup()
      			.addContainerGap()
      			.addGroup(layout.createParallelGroup(Alignment.TRAILING)
      				.addGroup(layout.createSequentialGroup()
      					.addGroup(layout.createParallelGroup(Alignment.LEADING)
      						.addGroup(layout.createSequentialGroup()
      							.addGroup(layout.createParallelGroup(Alignment.LEADING)
      								.addComponent(jPanel7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
      								.addComponent(jPanel3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
      								.addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, 246, GroupLayout.PREFERRED_SIZE)
      								.addComponent(jPanel4, GroupLayout.PREFERRED_SIZE, 230, GroupLayout.PREFERRED_SIZE))
      							.addPreferredGap(ComponentPlacement.RELATED, 70, Short.MAX_VALUE)
      							.addGroup(layout.createParallelGroup(Alignment.LEADING)
      								.addGroup(Alignment.TRAILING, layout.createSequentialGroup()
      									.addComponent(btnDealer, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
      									.addPreferredGap(ComponentPlacement.UNRELATED)
      									.addGroup(layout.createParallelGroup(Alignment.BASELINE)
      										.addComponent(jButtonSit, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
      										.addComponent(jButtonStart, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE))
      									.addGap(4))
      								.addGroup(Alignment.TRAILING, layout.createSequentialGroup()
      									.addComponent(btnNieWymieniam)
      									.addPreferredGap(ComponentPlacement.RELATED)
      									.addGroup(layout.createParallelGroup(Alignment.BASELINE)
      										.addComponent(btnChangeCards)
      										.addComponent(playerLastMove[5], GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)))
      								.addGroup(Alignment.TRAILING, layout.createSequentialGroup()
      									.addGroup(layout.createParallelGroup(Alignment.BASELINE)
      										.addComponent(dealerInf[5], GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
      										.addComponent(playerName[5], GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE))
      									.addGap(2)
      									.addComponent(playerCash[5])
      									.addGap(24))))		
      						.addGroup(layout.createSequentialGroup()
      							.addComponent(jPanel8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
      							.addGap(18)
      							.addComponent(jPanel6, GroupLayout.PREFERRED_SIZE, 195, GroupLayout.PREFERRED_SIZE)))
      					.addPreferredGap(ComponentPlacement.RELATED)
      					.addGroup(layout.createParallelGroup(Alignment.TRAILING)
      						.addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE)
      						.addGroup(layout.createSequentialGroup()
      							.addGroup(layout.createParallelGroup(Alignment.TRAILING)
      								.addComponent(jPanel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
      								.addGroup(layout.createSequentialGroup()
      									.addGap(10)
      									.addGroup(layout.createParallelGroup(Alignment.BASELINE)
      										.addComponent(jButtonFold, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
      										.addComponent(jButtonAllIn, GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE))
      									.addGap(14)
      									.addGroup(layout.createParallelGroup(Alignment.BASELINE)
      										.addComponent(jButtonCall, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
      										.addComponent(jButtonCheck, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
      									.addPreferredGap(ComponentPlacement.RELATED)
      									.addGroup(layout.createParallelGroup(Alignment.BASELINE)
      										.addComponent(jButtonRaise, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
      										.addComponent(jButtonBet, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))))
      							.addGap(30)))
      					.addContainerGap())
      				.addGroup(layout.createSequentialGroup()
      					.addComponent(jPanel5, GroupLayout.PREFERRED_SIZE, 193, GroupLayout.PREFERRED_SIZE)
      					.addGap(264))))
      );

      
      
      jScrollPane1.setViewportView(gameLog);
      


            
      getContentPane().setLayout(layout);

      pack();
  }
  
  //obsługa żądania wyjścia z menu

  private void jMenuItemExitActionPerformed(final ActionEvent event) {
  	Controler.guiExScr= new ExitScreen(contr);
  	 
  	//Init
  	Controler.guiExScr.setVisible(true);
  	Controler.guiExScr.setLocationRelativeTo(null);
      
    

  }

  
  
  /**
   * 
   * Reakcja na opuszczenie stołu
   * 
   * @param evt
   */
  private void  mntmOpuStActionPerformed(final ActionEvent event) 
  {
	  Object[] options = {"Tak", "Nie"};
	  
	  int exitChose = JOptionPane.showOptionDialog(null, 
		          "Na pewno chcesz wyjść?", 
		          "Wyjście", 
		          JOptionPane.YES_NO_OPTION, 0,null,options, options[1]);
	  
	  if (exitChose == JOptionPane.YES_OPTION) {
		  
		  
		 if(!contr.fSendData( leftTable() ))
		 {
			 Core_Gui_ErrorWindow.errorWindow(Core_Messages.getMessage(0x02F3), Core_Messages.getMessage(0x0100));
		 }
		  
		  
		  this.dispose();
			 
		  
		  //Uruchomienie okna z listą stołów
		 contr.guiTabListScr = new TableListScreen(contr);

		 contr.guiTabListScr.setVisible(true);
		 contr.guiTabListScr.setLocationRelativeTo(null);
	  }
	}
  //ustawiamy obrazek z literką "d" obok Dealera
  //dealerNumber = numer gracza, który jest dealerem
  //zakładam, że będzie jakaś tablica stringów z nazwami graczy i indeks właśnie będzie numerem
  
  public void chooseDealer(final int dealerNumber){
  
	  switch(dealerNumber) {
		  case 1: setVisiableDealerButton(0); break;
		  case 2: setVisiableDealerButton(1); break;
		  case 3: setVisiableDealerButton(2); break;
		  case 4: setVisiableDealerButton(3); break;
		  case 5: setVisiableDealerButton(4); break;
		  case 6: setVisiableDealerButton(5); break;
	  default:;
	  }
  }
  
  
  /**
   * Pokazuje przycisk dealera
   * 
   * [0]
   * -1 oznacza schowanie wszystkich dealerów
   * 
   * @param id
   */
  private void setVisiableDealerButton(int id)
  {
	  //chowa poprzedni
	  if(dealerNumber !=  -1)
	  {
		  dealerInf[ dealerNumber ].setVisible(false);
	  }
	  
	  
	  if(id !=  -1)
	  {
		  //Aktywuje aktualny
		  dealerInf[ id ].setVisible(true);
	  }
	  
	  
	  //Zapamiętanie kto jest dealerem teraz
	  dealerNumber = id;
	  
	  
  }
  
  
  
  //wyswietlamy czas na ruch gracza

	 
	// public void giveElapsedTime(){
		 
// }
  
  //pozycje "pomoc" i "o autorach"

  private void jMenuItemHelpActionPerformed(ActionEvent evt) {
      
  }

  private void jMenuItemAboutActionPerformed(ActionEvent evt) {
      final AboutAuthorsScreen about = new AboutAuthorsScreen();
          about.setVisible(true);
          about.setLocationRelativeTo(null);
  }

  //informowanie serwera o zamiarach gracza - co nacisnął
  
  private void jMenuItemStartActionPerformed(ActionEvent evt) 
  {
	  if(!contr.fSendData( isReady() ))
	  {
	  	Core_Gui_ErrorWindow.errorWindow(Core_Messages.getMessage(0x02F3), Core_Messages.getMessage(0x0100));
	  }
  }

 
  
  /**
   * Event z przycisku dot. zasiadania przy stole
   * 
   * @param evt
   */
  private void jButtonSitActionPerformed(ActionEvent evt) {
	  sitAction();
  }
  
  
  /**
   * Event z menu dot. zasiadania przy stole
   * 
   * @param evt
   */
  private void jMenuItemSitActionPerformed(ActionEvent evt) {
	  sitAction();
  }
  
  
  /**
   * Funkcja wykonuje operacje, gdy obserwator chce zasiąść przy stole
   * 
   */
  private void sitAction()
  {
  	if(!contr.fSendData( migrationInTable(1) ))
  	{
  		Core_Gui_ErrorWindow.errorWindow(Core_Messages.getMessage(0x02F3), Core_Messages.getMessage(0x0100));
  	}
  }
  
  
  
	/**
	 * Ustawia obraz kart gracza lub inne
	 * 
	 * <pre>
	 * data:
	 * [0]
	 * 0 - Karty Gracza
	 * 1 - Karty Innej osoby
	 * [1]
	 * nr karty gracza od [1, 4]
	 * </pre>
	 * 
	 * 
	 * @param id
	 */
	public void setCardImage(int[] data, JLabel obj, String name)
	{
		if(data[0] == 0) //Gracz
		{
			JLabel obj2 = getLabelPlayerCards( data[1] );
			
			obj2.setIcon(new javax.swing.ImageIcon(getClass().getResource(RESOURCE_DIR + name + ".png")));
		}
		else if(data[0] == 1) //Inne
		{
			obj.setIcon(new javax.swing.ImageIcon(getClass().getResource(RESOURCE_DIR + name + ".png")));
		}
	}
  
  
  /**
   * Przygotowuje środowiska w zależności od stanu gracza
   * 
   */
  public  final void prepareEnviroment()
  {
	  if(gameStart == true) //Gra w toku
	  {
		  if(gameState[0] == 0) //Obserwator
		  {
		      jMenuItemStart.setEnabled(false);
		      jMenuItemSit.setEnabled(false);
		      jButtonStart.setVisible(false);
			  jButtonSit.setVisible(false);
			  
			  
			  if(gameState[2] == 1) //Player Leave
			  {
				  jButtonFold.setEnabled(false); 
		          jButtonAllIn.setEnabled(false);
		          jButtonBet.setEnabled(false);
		          jButtonCall.setEnabled(false);
		          jButtonCheck.setEnabled(false);
		          jButtonRaise.setEnabled(false); 
		          
		          
	    		  btnNieWymieniam.setVisible(false);
	    		  btnNieWymieniam.setEnabled(false);
	    		  btnChangeCards.setVisible(false);
	    		  btnChangeCards.setEnabled(false);
	    		  
	    		  btnDealer.setVisible(false);
	    		  btnDealer.setEnabled(false);
		          
			  }
			  
			  
		  }
		  else if(gameState[0] == 1) //Gracz
		  {

		      if(gameState[1] == 0) //Inicjalizacja gry
		      {
			      //Przyciski Usiądź / Start
			      jMenuItemStart.setEnabled(false);
			      jButtonStart.setVisible(false);
			      
			      //Najbardziej ulubiony przucisk pokerzysty
			      jButtonFold.setEnabled(true);
		      }
		      else if(gameState[1] == 1) //mój ruch
		      {
		    	  if(gameState[2] == 0) //Start
		    	  {
			          jButtonAllIn.setEnabled(true);
			          jButtonBet.setEnabled(true);
			          jButtonCall.setEnabled(true);
			          jButtonCheck.setEnabled(true);
			          jButtonRaise.setEnabled(true);
		    	  }
		    	  else
		    	  {
			          jButtonAllIn.setEnabled(false);
			          jButtonBet.setEnabled(false);
			          jButtonCall.setEnabled(false);
			          jButtonCheck.setEnabled(false);
			          jButtonRaise.setEnabled(false); 
		    	  }

		      }
		      else if(gameState[1] == 2) //Wymiana kart
		      {
		    	  if(gameState[2] == 0) //Start
		    	  {
		    		  
		    		  
		    		  
		    		  
		    		  btnNieWymieniam.setEnabled(true);
		    		  btnNieWymieniam.setVisible(true);
		    		  btnChangeCards.setEnabled(true);
		    		  btnChangeCards.setVisible(true);
		    	  }
		    	  else
		    	  {
		    		  btnNieWymieniam.setVisible(false);
		    		  btnNieWymieniam.setEnabled(false);
		    		  btnChangeCards.setVisible(false);
		    		  btnChangeCards.setEnabled(false);
		    	  }
		          
		          
		      }
		      else if(gameState[1] == 3) //Dealer
		      {
		    	  if(gameState[2] == 0) //Start
		    	  {
		    		  btnDealer.setEnabled(true);
		    		  btnDealer.setVisible(true);
		    	  }
		    	  else
		    	  {
		    		  btnDealer.setVisible(false);
		    		  btnDealer.setEnabled(false);
		    	  }
		      }
		      else if(gameState[1] == 4) //Licytacja (EndGame)
		      {
		    	  
		      }


	      
		      
		  }
	  }
	  else //Cisza przed burzą
	  {
		  
		  jButtonFold.setEnabled(false); 
          jButtonAllIn.setEnabled(false);
          jButtonBet.setEnabled(false);
          jButtonCall.setEnabled(false);
          jButtonCheck.setEnabled(false);
          jButtonRaise.setEnabled(false);
		 
		  
		  btnNieWymieniam.setVisible(false);
		  btnNieWymieniam.setEnabled(false);
		  btnChangeCards.setEnabled(false);
		  btnChangeCards.setVisible(false);
		  
		  btnDealer.setVisible(false);
		  btnDealer.setEnabled(false);
		  
		  
		  if(gameState[0] == 0) //Obserwator
		  {
			  //Aktywacja / Dezaktywacja poszczególnych przycisków
		      jMenuItemStart.setEnabled(false);
		      jMenuItemSit.setEnabled(true);
		      jButtonStart.setVisible(false);
			  jButtonSit.setVisible(true);
		  }
		  else if(gameState[0] == 1) //Gracz
		  {
		      //Aktywacja / Dezaktywacja poszczególnych przycisków
	    	  if(gameState[2] == 0) //Start
	    	  {
			      jMenuItemStart.setEnabled(true);
			      jMenuItemSit.setEnabled(false);
			      jButtonStart.setVisible(true);
			      jButtonSit.setVisible(false);
	    	  }
	    	  else
	    	  {
	    		  jMenuItemStart.setEnabled(false);
			      jButtonStart.setVisible(false);
	    	  }
		      
		  }
	  }
  }
  
  
  
  
  
  
  /**
   * Przycisk przesyła informacje o gotowości gracza do gry
   * 
   * 
   * @param evt
   */
  private void jButtonStartActionPerformed(final ActionEvent evt) {
	  if(!contr.fSendData( isReady() ))
	  {
	  	Core_Gui_ErrorWindow.errorWindow(Core_Messages.getMessage(0x02F3), Core_Messages.getMessage(0x0100));
	  }
  }
   

  
  
  //=============================================
  

  /**
   * Akcja dla przycisku Bet (0x01)
   * 
   * @param evt
   */
  private void jButtonBetActionPerformed(final ActionEvent event)
  {
	  
	  final Raise RaiseWindow = new Raise(1);
	  RaiseWindow.setVisible(true);
	  RaiseWindow.setLocationRelativeTo(null);
	  
	  if(RaiseWindow.getPlayerBid() > 0)
	  {
		  if(contr.fSendData( playerMoveData(0x01, new Object[] { RaiseWindow.getPlayerBid() }) ))
		  {
			  //Ustawienie końca etapu ruchu
			  gameState[2] = 1;
			  prepareEnviroment();  
		  }
		  else
		  {
			  Core_Gui_ErrorWindow.errorWindow(Core_Messages.getMessage(0x02F3), Core_Messages.getMessage(0x0100));
		  } 
	  }
	  
	  
  }
  
  /**
   * Akcja dla przycisku Check (0x02)
   * 
   * @param evt
   */
  private void jButtonCheckActionPerformed(final ActionEvent event) {
	  //TODO: blokada interfejsu
	  gameState[2] = 1;
	  this.prepareEnviroment();
	  
	  if(contr.fSendData( playerMoveData(0x02, null) ))
	  {
		  //Ustawienie końca etapu ruchu
		  gameState[2] = 1;
		  prepareEnviroment();  
	  }
	  else
	  {
		  Core_Gui_ErrorWindow.errorWindow(Core_Messages.getMessage(0x02F3), Core_Messages.getMessage(0x0100));
	  }    
  }

  
  /**
   * Akcja dla przycisku Raise (0x03)
   * 
   * @param evt
   */
  private void jButtonRaiseActionPerformed(ActionEvent evt) {
	  
	  
	  final Raise RaiseWindow = new Raise(2);
	  RaiseWindow.setVisible(true);
	  RaiseWindow.setLocationRelativeTo(null);
	  
	  if(RaiseWindow.getPlayerBid() > 0)
	  {
		  if(contr.fSendData( playerMoveData(0x03, new Object[] { RaiseWindow.getPlayerBid() }) ))
		  {
			  //Ustawienie końca etapu ruchu
			  gameState[2] = 1;
			  prepareEnviroment();  
		  }
		  else
		  {
			  Core_Gui_ErrorWindow.errorWindow(Core_Messages.getMessage(0x02F3), Core_Messages.getMessage(0x0100));
		  } 
	  }
	  

}  
  
  
  /**
   * Akcja dla przycisku call (0x04)
   * 
   * @param evt
   */
  private void jButtonCallActionPerformed(final ActionEvent event) {
	  //TODO: blokada interfejsu
	  gameState[2] = 1;
	  this.prepareEnviroment();
	  
	  if(contr.fSendData( playerMoveData(0x04, null) ))
	  {
		  //Ustawienie końca etapu ruchu
		  gameState[2] = 1;
		  prepareEnviroment();  
	  }
	  else
	  {
		  Core_Gui_ErrorWindow.errorWindow(Core_Messages.getMessage(0x02F3), Core_Messages.getMessage(0x0100));
	  } 
  }
  
  
  /**
   * Akcja dla przycisku fold (0x05)
   * 
   * @param evt
   */
  private void jButtonFoldActionPerformed(final ActionEvent event) {
	  
	  Object[] options = {"Tak", "Nie"};
	  int whatChose = JOptionPane.showOptionDialog(null, 
              "Na pewno chcesz spasować?", 
              "Potwierdzenie", 
              JOptionPane.YES_NO_OPTION, 0,null,options, options[1]); 
	  if (whatChose == JOptionPane.YES_OPTION) {
		  if(contr.fSendData( playerMoveData(0x05, null) ))
		  {
			  //Ustawienie końca etapu ruchu
			  gameState[2] = 1;
			  prepareEnviroment();  
		  }
		  else
		  {
			  Core_Gui_ErrorWindow.errorWindow(Core_Messages.getMessage(0x02F3), Core_Messages.getMessage(0x0100));
		  }
	  }
	   
	    	
     
  }
  
  

  
  
  /**
   * Akcja dla przycisku AllIn (0x06)
   * 
   * @param evt
   */
  private void jButtonAllInActionPerformed(final ActionEvent event) {
	  
	  //TODO: blokada interfejsu
	  gameState[2] = 1;
	  this.prepareEnviroment();
	  
	  if(contr.fSendData( playerMoveData(0x06, null) ))
	  {
		  //Ustawienie końca etapu ruchu
		  gameState[2] = 1;
		  prepareEnviroment();  
	  }
	  else
	  {
		  Core_Gui_ErrorWindow.errorWindow(Core_Messages.getMessage(0x02F3), Core_Messages.getMessage(0x0100));
	  }
	  
	  
  }
  
  
  

  
  
  
  //=============================================
  
  
  
  
  
  


  /**
   * Funkcja zajmującą się przesuwanie kart
   * 
   * 
   * @param index
   * @param label
   */
  synchronized void chooseCardsToExchange(int index) {
 
	if(this.myTurn)
	{
		
	  //Stan wcześniejszy karty (Karta była podniesiona)
	  if(accessMod[index] == 0) {
		  labelPlayerCards[ index ].setLocation(labelPlayerCards[ index ].getX(), labelPlayerCards[ index ].getY() + 10);

	      accessMod[index] = 1;
	      suma -= accessMod[index];
	      
	      

	  }
	  	else //(Karta nie była podniesiona)
	  {
	  		labelPlayerCards[ index ].setLocation(labelPlayerCards[ index ].getX(), labelPlayerCards[ index ].getY() - 10);
	      
	      suma += accessMod[index];
	      accessMod[index] = 0;
	  } 
	}
 

}

  //=============================================
 
  //wywołujemy metodę dla pól jLabelów przeznaczonych na karty gracza
  //evt.getSource -> pobiera źródło żądania (Czyli skąd wyszło)
  
  private void labelPlayerCards0MouseClicked(java.awt.event.MouseEvent evt) {
      chooseCardsToExchange(0);
  }

  private void labelPlayerCards1MouseClicked(java.awt.event.MouseEvent evt) {
      chooseCardsToExchange(1);
  }

  private void labelPlayerCards2MouseClicked(java.awt.event.MouseEvent evt) {
      chooseCardsToExchange(2);
  }

  private void labelPlayerCards3MouseClicked(java.awt.event.MouseEvent evt) {
      chooseCardsToExchange(3);
      
  }
  
  
  
  //=============================================
  

  // wysyłamy karty do wymiany - jeżeli accessMod[i] jest gdzieś równy 0 to znaczy że kart jest wysunięta
  // pod accessMod[0] mamy kartę umieszczoną w labelPlayerCards[0], accessMod[1] to labelPlayerCards[1], accessMod[2] to labelPlayerCards[2], accessMod[3] to labelPlayerCards[3]
  
  /**
   * Wysyłam karty do wymiany
   * 
   * @param accessMod
   * @return
   */
  public Object[] exchangeCards(){
	  
	  
	  //Def.
	  Object[] cData;
	  
	  //Init.
	  cData = new Object[suma];


	  
	  int k = 0;
	  
	  //Uwaga dot. referencji
	  for(int i = 0; i < suma; i++)
	  {
		  //Pobiera własności kart
		  if(accessMod[i] == 0)
		  {
			  cData[k] = i;
			  
			  ++k;
		  }
	  }
	  
	  
	  return cData;
  }
  
  
  /**
   * Ustawia nowe karty
   * 
   * @param playerDeck
   */
  public void updateCards(Object[] playerDeck)
  {
	  int k = 0;
	  
	  //Uwaga dot. referencji
	  for(int i = 0; i < suma; i++)
	  {
		  //Pobiera własności kart
		  if(accessMod[i] == 0)
		  {
			  setCardImage(new int[] {0, i}, null, (String) playerDeck[k]);
			  
			  
			  //Ustawienie Informacji + zsunięcie karty
			  accessMod[i] = 1;
			  
			  //Operacja zsuwania
			  chooseCardsToExchange(i);
			  
			  
			  ++k;
		  }
	  }
	  
  }
  
  
  
  //====================
  

  /**
   * Formatuje wygląd kasy
   * 
   * @param cash
   * @return
   */
  private String updateCash(String cash)
  {
	  return cash + " " + Core_Messages.getMessage(0x0000);
  }
  
  
  /**
   * Dodaje nowej wiadomości do gameLogu
   * 
   * <pre>
   * {ndata}
   * [0] typ wiadomości
   * 0 - Server Msg
   * 1 - Player Msg
   * 2 - Pure Server MSG
   * 
   * [1] (Wpisać byle co, gdy param [0] różny od 1)
   * - Identyfikator gracza pozycja
   * </pre>
   * 
   * 
   * @param ndata
   * @param str
   */
  public void apendNewMessage(final int[] ndata, final String str)
  {
	  //Def.
	  String modif;
	  
	  //Init.
	  modif = "";
	  
	  
	  if(ndata[0] == 1)
	  {
		  modif = playerName[ ndata[1] ].getText() +  " - " + str;
	  }
	  else if(ndata[0] == 0)
	  {
		  modif = str;
	  }
	  else if(ndata[0] == 0)
	  {
		  modif = "| Server | - " + str;
	  }
	  
	  gameLog.setText( modif + "\n" + gameLog.getText() );
  }
  
   
  
  
  //===================
  
  
  
  
  
 /**
  * 
  *   * Tłumaczy kodu ruchów na text
  * 
  * @param nickname
  * @param moveData
  * @return
  */
 public String moveTranslator( Integer[] moveData)
 {
	 //Def.
	 String tmp;
	 
	 
	 switch(moveData[0])
	 {
	 	case 1: //Bet
	 		tmp = Core_Messages.getMessage(0x03C9);
		break;
		
	 	case 2: //Check
	 		tmp = Core_Messages.getMessage(0x03C8);
		break;
		
	 	case 3: //Raise
	 		tmp = Core_Messages.getMessage(0x03C7);
		break;
		
	 	case 4: //Call
	 		tmp = Core_Messages.getMessage(0x03C6);
		break;
		
	 	case 5: //Fold
	 		tmp = Core_Messages.getMessage(0x03C5);
		break;
		
	 	case 6://AllIn
	 		tmp = Core_Messages.getMessage(0x03C4);
		break;
		
	 	case 7: //Dealer
	 		tmp = Core_Messages.getMessage(0x03C2);
	 	break;
	 	
	 	case 8: //Card Exchange
	 		tmp = Core_Messages.getMessage(0x03C1);
	 	break;
	 	
	 	case 9: //Informacja o rozpoczęciu tury
	 		tmp = Core_Messages.getMessage(0x03C0);
	 	break;
		
		default: //Brak ruchu
			tmp = Core_Messages.getMessage(0x03C3);
		break;
		
	 }
	 
	 
	 
	 
	 //Czyli czy argument zawiera dane o ruchu
	 if(moveData[0] == 1 || moveData[0] == 3 || moveData[0] == 4)
	 {
		 tmp+= moveData[1] + Core_Messages.getMessage(0x0000);
	 }
	 
	 
	 return tmp;
 }
  
  
  
  
  
 /**
  * Ustawia dane w stole (Taki mass wywoływacz)
  * 
  * [type]
  * 0 - Player Ruch
  * 1 - Nowe Rozdanie
  * 2 - Full
  * 
  * @param data
  * @param type
  */
 public void setTableData(Object[] data, Integer type)
 {
	 if(type == 0) //Player Ruch
	 {
		 updateStatusLabel( Core_JsonDataSchema.toIntStr(data[0]) , 3); //Pot
	 }
	 else if(type == 1) //Nowe Rozdanie
	 {
		 updateStatusLabel(Core_JsonDataSchema.toIntStr(data[0]), 0); //Rozdanie
	 }
	 else if(type == 2) //Full
	 {
		 updateStatusLabel( Core_JsonDataSchema.toIntStr(data[0]) , 3);
		 gameStart = (boolean) data[1];
		 updateStatusLabel((String) data[2], 2);
		 updateStatusLabel((String) data[3], 1);
		 updateStatusLabel(Core_JsonDataSchema.toIntStr(data[4]).toString(), 0);
		 
		 //Ustawia pozycje gracza przy Stole
		 Player_Constans.setNrOfPosition( Core_JsonDataSchema.toInt(data[5]) );
	 }
 }
  
 
 
 
 
 
  
 
 
 
/**
 * 
 * Aktualizuje wybrane labele
 * 
 * <pre>
 * [nr]
 * 0 - Ilość rozdań
 * 1 - Dealer [Nickname]
 * 2 - Admin [NickName]
 * 3 - Pot
 * </pre>
 * 
 * @param text
 * @param nr
 */
public void updateStatusLabel(String text, int nr)
{
	if(nr == 0)
	{
	  nrOfDeal.setText("Ilość rozdań: " + text );
	}
	else if(nr == 1)
	{
	  dealerName.setText("Dealer: " + text );
	}
	else if(nr == 2)
	{
	  adminName.setText("Admin: " + text );
	}
	else if(nr == 3)
	{
	  potValue.setText( this.updateCash( text )  );
	}
}
  

  
  /**
   * 
   * Ustawia Login, Kasę, Last Action (Dla wielu graczy)
   * 
   * 
   * <pre>
   * 	{data} (Schemat... tutaj mamy taki ciąg [0, 3], [4, 7], ...)
   * 		[0] - Pozycja w tabeli (Przy Stole)
   * 		[1] - NickName
   * 		[2] - Stan Konta gracza
   * 		[3] - Zakład gracza
   * 		[4] - ruch ID
   * 		[5] - ruch value
   * 
   * 	{mode} - Tryb pracy
   * 		[0] - Uzupełnienie stołu (jak gracz się loguje)
   * 		[1] - Dosiadł się do stołu (jako player)
   * 		[2] - Odszedł od stołu (jako player)
   * 	
   * </pre>
   * 
   * 
   * 
   * 
   * @param mode
   * @param tdata
   * @param id
   */
  public void setPlayerData(final Object[] data, final int mode)
  {
	  //Def.
	  int pos;
	  Integer tmp[];
	  
	  	  //Init
	  	  pos = 0;
	  	  tmp = new Integer[2];
	  	  
	  	  
	  
	  
		  //Aktualizujemy dla wybranych identyfikatorów
		  for(int i = 0; i < data.length; ++i)
		  {
			  //Sprawdzenie czy nie tzw. błędu na wejściu
			  if(data[i] != null)
			  {
				  pos = calcInternalPosition( Core_JsonDataSchema.toInt(data[i]) );
				  
				  //(String) data[++i] 
				  playerName[ pos ].setText( (String) data[++i]  );
				  playerCash[ pos ].setText(updateCash( Core_JsonDataSchema.toIntStr(data[++i])  ));
				  playerBid[ pos ].setText( updateCash( Core_JsonDataSchema.toIntStr(data[++i]) ));
				  
				  
				  
				  tmp[0] = Core_JsonDataSchema.toInt(data[++i]);
				  tmp[1] = Core_JsonDataSchema.toInt(data[++i]);
				  
				  playerLastMove[ pos ].setText( moveTranslator( tmp));
			  }
			  else
			  {
				  //przesunięcie
				  i += 4;
			  }
			  
		  }
		  
		  
		  //Jak pętla zadziałała, + odpowiedni tryb
		  if(mode != 0)
		  {
			  this.apendNewMessage(new int[] {1, pos}, Core_Messages.getMessage(0x03DB));
		  }
  }
  
  
  
  /**
   * <pre>
   * {data} - schemat
   * 		[0] - Pozycja w tabeli
   * 		[1] - NickName
   * 		[2] - ruch ID
   * 		[3] - ruch value
   * </pre>
   * 
   * @param data
   */
  public void setGamerData(final Object[] data)
  {
	  
	  //def. 
	  int 	pos;
	  Integer tmp[];
	  
	  //Init
	  tmp = new Integer[2];
	  
	  
	  pos = 5; //calcInternalPosition( Core_JsonDataSchema.toInt(data[i]) )
	  
	  //(String) data[++i] 
	  playerName[ pos ].setText( Player_Constans.getPlayerNickname() ); //(String) data[++i] 
	  playerCash[ pos ].setText(updateCash( Integer.toString(Player_Constans.getCash())  )); //Core_JsonDataSchema.toIntStr(data[++i])
	  playerBid[ pos ].setText(updateCash( "0" )); //BidGracza
	  
	  
	  tmp[0] = -1;//Core_JsonDataSchema.toInt(data[++i]);
	  tmp[1] = 0;//Core_JsonDataSchema.toInt(data[++i]);
	  
	  playerLastMove[ pos ].setText( moveTranslator( tmp));
  }
  
  
  /**
   * Czyści wpis gracza w stole
   * 
   * Tryb pracy:
   * <pre>
   * 	[0] - Domyslny
   * 	[1] - Bez korekcji pozycji
   * </pre>
   * 
   * @param pos identyfikator gracza
   */
  public void clearPlayerData(final int pos, int mode)
  {
	  
	  //def. tmp
	  int tmp;
	  
	  //init
	  if(mode == 0)
	  {
		  tmp  = calcInternalPosition(pos);
	  }
	  else
	  {
		  tmp = pos;
	  }

	  this.apendNewMessage(new int[] {1, tmp}, Core_Messages.getMessage(0x03DA));
	  
	  playerName[ tmp ].setText( Core_Messages.getMessage(0x0001)  );
	  playerCash[ tmp ].setText( this.updateCash("0"));
	  playerBid[ tmp ].setText( this.updateCash("0"));
	  playerLastMove[ tmp ].setText( Core_Messages.getMessage(0x0001) );
	  
	  
	  
  }

  
  
  /**
   * Przekształca zbiór wynikowych pozycji na pozycje wew. stołu
   * 
   * @param pos
   * @return
   */
  public int calcInternalPosition(final int pos)
  {
	  int tmp;
	  
	  if(gameState[0] == 1) //Gracz
	  {
		  if (Player_Constans.getNrOfPosition() < pos)
		  {
			  tmp = pos - (Player_Constans.getNrOfPosition() + 1);
		  }
		  else if (Player_Constans.getNrOfPosition() > pos)
		  {
			  tmp = pos + (Player_Constans.getNrOfPosition() - 1); 
		  }
		  else
		  {
			  tmp = 5; /** Pozycja Gracza */
		  }
	  }
	  else //Obserwator lub Twórca stołu
	  {
		  tmp = pos;
	  }
	  
	  return tmp;
  }
  
  

  
  
  /**
   * Funkcja się zajmuje aktualizacją ruchu jednego gracza
   * 
   * 
   * <pre>
   * nData
   * [0]
   * 0 - Aktwyny gracz (Zmiana koloru)
   * 1 - Aktualizacja danych graczy (dla ruchów raise, call, bet)
   * 2 - Fold
   * 3 - AllIn
   * 4 - Ruchy bez znaczenia (Dealer, rozdanie kart)
   * 
   * [1]
   * Pozycja gracza
   * 
   * 
   * 
   *  aData:
   *  
   *  
   *  length - 2 - Kod ruchi
   *  length - 1 - wartość ruchu
   * 
   * </pre>
   * 
   * 
   * @param id Pozycja gracza w stole
   * @param aData
   */
  public void updatePlayerAction(int[] nData, Object[] aData)
  {
	  //Przemyśleć działanie
	  
	  //TODO na inty.
	  
	  if (nData[0] == 0) //Aktywny gracz
	  {

		  playerName[ nData[1] ].setForeground(Color.blue);

	  }
	  else if(nData[0] == 1) //Ruch typowy raise, bed, check, call
	  {
		  /**
		   * 
		   * 0 - Cash
		   * 1 - Bid
		   * 2 - Pot
		   * 
		   */
		  
		  
		  playerName[ nData[1] ].setForeground(Color.black);
		  
		  
		  playerCash[ nData[1] ].setText(updateCash( Core_JsonDataSchema.toIntStr( aData[0] ) )  );
		  
		  playerBid[ nData[1] ].setText(updateCash( Core_JsonDataSchema.toIntStr( aData[1] ) )  );
		  
		  //Ustawia stołu Pot
		  updateStatusLabel( Core_JsonDataSchema.toIntStr( aData[2]) , 3);
	  }
	  else if (nData[0] == 2) //Fold //Rage QUit
	  {
		  
		  playerName[ nData[1] ].setForeground(Color.gray);
	  }
	  else if(nData[0] == 3) //ALL:IN
	  {
		  
		  playerCash[ nData[1] ].setText(updateCash( Core_JsonDataSchema.toIntStr( aData[0] ) )  );
		  
		  playerBid[ nData[1] ].setText(updateCash( Core_JsonDataSchema.toIntStr( aData[1] ) )  );
		  
		  //Ustawia stołu Pot
		  updateStatusLabel( Core_JsonDataSchema.toIntStr( aData[2]) , 3);
		  
		  playerName[ nData[1] ].setForeground(Color.orange);
	  }
	  else if (nData[0] == 4)
	  {
		  //ruchy bez znaczenia
	  }
	  
	  
	  //Ustawia Komunikat
	  playerLastMove[ nData[1] ].setText( moveTranslator(new Integer[] {
		Core_JsonDataSchema.toInt(aData.length - 2),
		Core_JsonDataSchema.toInt(aData.length - 1)
	  }));
	  
	  //Append do GameLoga
	  apendNewMessage( new int[] {1, nData[1]}, playerLastMove[ nData[1] ].getText()  );

  }
  
  
  
  
  /**
   * 
   * Aktualizacja numerów dealer pos
   * 
   */
  public void setDealer(int dealerPos)
  {
	  //Usunięcie znacznika z starego Dealera.
	  
	  //Dodanie znacznika na nowy
	  
	  dealerNumber = dealerPos;
	  
  }
  
  
  
  /**
   * 
   * Ustawia czy gracz jest obserwatorem czy graczem
   * 
   */
  public void setPlayerType(final Integer type)
  {
	  gameState[0] = type;
	  
	  //Aktualizuje wygląd stołu
	  this.prepareEnviroment();
  }
  
  
  
  
  	/**
  	 * Ustawia Stan gry
  	 * 
  	 * <pre>
  	 * [1] - stan gry
  	 * 	0 - inicjalizacja gry
  	 * 	1 - mój ruch
  	 * 	2 - wymiana kart
  	 * 	3 - Dealer
  	 * 	4 - Licytacja
  	 * </pre>
  	 * 
  	 * @param mState
  	 */
	public void setGameState(final int mState)
	{
		 gameState[1] = mState;
		 this.prepareEnviroment();
	}
  
  
	/**
	 * Czynność ustawia flagi informującej o ukończeniu czynności
	 * [0]
	 *  0 - start
	 *  1 - koniec
	 * 
	 * @param mState
	 */
	public void setPlayerMoveEndStatus(int mState)
	{
		 gameState[2] = mState;

	}
  
	
	
	/**
	 * Ustawia flagę czy gra się zaczeła T/F
	 * 
	 * @param status
	 */
	public void setGameStart(boolean status)
	{
		gameStart = status;
		this.prepareEnviroment();
	}
	
  
  
  
  /**
   * Zwraca label uosabiający odpowiednią kartę
   * 
   * @param id
   * @return
   */
  public JLabel getLabelPlayerCards(int id)
  {
	  return labelPlayerCards[id];
  }
  
  
  //TODO: Ekran porównania kart.
  //TODO: Reset Licytacji
  //TODO: Inne


  
  
  //=====================
  
  
  /**
   * Migracja wew. stołu
   * 
   * [0] mode
   * 0 - Obserawatir
   * 1 - Gracz
   * 
   * @param mode
   * @return
   */
  private Core_JsonDataSchema migrationInTable(final int mode)
  {
  	Core_JsonDataSchema data = new Core_JsonDataSchema();
  	
  	//Ustawienie kodu
  	data.setKod(0x04);
  	
  	//Ustawienie argumentów
  	data.setArgs
  	(
  			new int[] {
  					0x05,
  					mode
  			}
  	);
  	
  	
  	//Ustawienie danych
  	data.setDataD1
  	(
  		new Object[] {
  			Player_Constans.getPlayerID(),
  		}
  	);
  	
  	return data;
  	
  }
  
  
  /**
   * Gracz opuszcza stół
   * 
   * @param mode
   * @return
   */
  private Core_JsonDataSchema leftTable()
  {
  	Core_JsonDataSchema data = new Core_JsonDataSchema();
  	
  	//Ustawienie kodu
  	data.setKod(0x04);
  	
  	//Ustawienie argumentów
  	data.setArgs
  	(
  			new int[] {
  				0x03
  			}
  	);
  	
  	
  	//Ustawienie danych
  	data.setDataD1
  	(
  		new Object[] {
  			Player_Constans.getPlayerID(),
  		}
  	);
  	
  	return data;
  	
  }
  
  
  
  /**
   * 
   * Przesyła informację do serwera o gotowości gracza do podjęcia wyzwania gry w badugi
   * 
   * @return
   */
  private Core_JsonDataSchema isReady()
  {
	  Core_JsonDataSchema data = new Core_JsonDataSchema();
	  
	  
	  	//Ustawienie kodu
	  	data.setKod(0x04);
	  	
	  	//Ustawienie argumentów
	  	data.setArgs
	  	(
	  			new int[] {
	  					0x04
	  			}
	  	);
	  	
	  	
	  	//Ustawienie danych
	  	data.setDataD1
	  	(
	  		new Object[] {
	  			Player_Constans.getPlayerID(),
	  		}
	  	);
	  
	  
	  return data;
  }
  
  
  /**
   * Tworzy struktórę zawierająca informację o ruchu gracza
   * 
   * [code]
   * Kod ruchu gracza (Patrz dokumentacja)
   * 
   * [inpData]
   * Zawiera dodatkowe dane
   * 
   * 
   * @param code
   * @param inpData
   * @return
   */
  private Core_JsonDataSchema playerMoveData(int code, Object[] inpData)
  {
	  Core_JsonDataSchema data = new Core_JsonDataSchema();
	  
	  	//Ustawienie kodu
	  	data.setKod(0x03);
	  	
	  	//Ustawienie argumentów
	  	data.setArgs
	  	(
	  		new int[] {
	  			0x01,
	  			code
	  		}
	  	);
	  	
	  	
	  	//Ustawienie danych do przesłania
	  	data.setDataD1( inpData );
	  
	  return data;
  }
  

}



